package com.ophiux.member.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ophiux.common.core.base.Result;
import com.ophiux.common.core.enums.ResultEnum;
import com.ophiux.member.mapper.MemberCardMapper;
import com.ophiux.member.model.dto.MemberCardDTO;
import com.ophiux.member.model.entity.MemberCard;
import com.ophiux.member.model.vo.MemberCardAddVo;
import com.ophiux.member.model.vo.MemberCardQueryVo;
import com.ophiux.member.model.vo.MemberCardUpdateVo;
import com.ophiux.member.service.IMemberCardService;
import com.ophiux.member.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class MemberCardServiceImpl extends ServiceImpl<MemberCardMapper, MemberCard> implements IMemberCardService {
    @Autowired
    IMemberService memberService;

    @Autowired
    MemberCardMapper memberCardMapper;



	@Override
	public List<MemberCardDTO> getList(IPage<MemberCard> iPage, MemberCardQueryVo queryDTO) {
		LambdaQueryWrapper<MemberCard> lambdaQueryWrapper = new LambdaQueryWrapper<MemberCard>();
    	lambdaQueryWrapper.eq(MemberCard::getMemberId, queryDTO.getMemberId());
		return null;
	}


    @Override
    public IPage<MemberCardDTO> getPage(IPage<MemberCard> iPage, MemberCardQueryVo queryDTO) {
    	LambdaQueryWrapper<MemberCard> lambdaQueryWrapper = new LambdaQueryWrapper<MemberCard>();
    	lambdaQueryWrapper.eq(MemberCard::getMemberId, queryDTO.getMemberId());
    	IPage<MemberCard> page = this.page(iPage,lambdaQueryWrapper);
        return null;
    }
    
    @Override
    public Result insert(MemberCardAddVo vo) {
        if (StringUtils.isEmpty(vo.getMemberId())) return Result.fail("账号信息不能为空");

        MemberCard data = JSONUtil.toBean(JSONUtil.toJsonStr(vo), MemberCard.class);
        int effCount = this.baseMapper.insert(data);
        if (effCount > 0) return Result.success();
        return Result.fail();
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Result insertBatch(List<MemberCardAddVo> list) {
		if(list == null || list.size()<1) return Result.fail(ResultEnum.PARAM_ERROR);
		LambdaQueryWrapper<MemberCard> queryWrapper = new LambdaQueryWrapper<MemberCard>();
		queryWrapper.eq(MemberCard::getMemberId,list.get(0).getMemberId());
		List<MemberCard> memberCardList = this.baseMapper.selectList(queryWrapper);
		if(memberCardList != null && memberCardList.size()>0) return Result.fail("您已经存在名片");
		
		List<MemberCard> reqList = new ArrayList<MemberCard>();
		for (MemberCardAddVo vo : list) {
            MemberCard data = JSONUtil.toBean(JSONUtil.toJsonStr(vo), MemberCard.class);
			reqList.add(data);
		}
		boolean isOk = saveBatch(reqList);
		if(!isOk) return Result.fail();
		
		return Result.success();
	}
    
    @Override
    public Result update(MemberCardUpdateVo vo) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result delete(String ids) {
        // TODO Auto-generated method stub
        return null;
    }



}
